<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">批量删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.userName" clearable placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" />
      <el-table-column label="id" prop="id" show-overflow-tooltip />
      <el-table-column label="用户名" prop="userName" show-overflow-tooltip />
      <el-table-column label="登录账号" prop="loginName" show-overflow-tooltip />
      <el-table-column label="手机号码" prop="phoneNumber" show-overflow-tooltip />
      <el-table-column label="密码" prop="password" show-overflow-tooltip />
      <el-table-column label="性别" show-overflow-tooltip>
        <template v-slot="scope">
          {{ formatSexType(scope.row.sex) }}
        </template>
      </el-table-column>
      <el-table-column label="用户类别" show-overflow-tooltip>
        <template v-slot="scope">
          {{ formatSpecialType(scope.row.userType) }}
        </template>
      </el-table-column>

      <el-table-column label="用户类型" show-overflow-tooltip>
        <template v-slot="scope">
          {{ formatHouseUserType(scope.row.houseUserType) }}
        </template>
      </el-table-column>

      <el-table-column label="账号状态" show-overflow-tooltip>
        <template v-slot="scope">
          <span :class="{ 'status-enable': scope.row.status === '0', 'status-disable': scope.row.status === '1' }">
            {{ formatStatusType(scope.row.status) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="权限" min-width="100px">
        <template #default="{ row }">
          <div class="tag-container">
            <el-tag v-for="(item, index) in row.permissions" :key="index" style="margin-left: 0" class="tag">
              {{ item.roleName }}
            </el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="修改时间" prop="updateTime" show-overflow-tooltip />
      <el-table-column label="操作" show-overflow-tooltip width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<style scoped>
  .tag-container {
    display: flex;
    flex-wrap: wrap;
    min-width: 100px;
  }

  .tag {
    margin-right: 8px;
    margin-bottom: 8px;
    white-space: normal; /* 允许内容换行 */
    display: inline-block; /* 确保每个 tag 在同一行内 */
  }

  .status-enable,
  .status-disable {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
  }

  .status-enable {
    border: 1px solid #6abf69; /* 绿色边框 */
    color: #6abf69; /* 绿色文本 */
  }

  .status-disable {
    border: 1px solid #f56c6c; /* 红色边框 */
    color: #f56c6c; /* 红色文本 */
  }
</style>

<script>
  import { doDelete, getList } from '@/api/userManagement'
  import { getRoleList } from '@/api/roleManagement'
  import Edit from './components/UserManagementEdit'
  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          username: '',
        },
        timeOutID: null,
      }
    },
    created() {
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          const newRow = JSON.parse(JSON.stringify(row))
          newRow.permissions = newRow.permissions.map((item) => item.id)
          this.$refs['edit'].showEdit(newRow)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        console.log('ids123', row.id)
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, totalCount } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      formatSpecialType(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === '0') {
          return '后台用户'
        } else if (type === '1') {
          return '普通业务用户'
        } else {
          return '其他'
        }
      },
      formatHouseUserType(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === '0') {
          return '房东'
        } else if (type === '1') {
          return '租客'
        } else if (type === '2'){
          return '同住人'
        }
      },
      formatSexType(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === '0') {
          return '男'
        } else if (type === '1') {
          return '女'
        } else {
          return '未知'
        }
      },
      formatStatusType(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === '0') {
          return '启用'
        } else if (type === '1') {
          return '停用'
        } else {
          return '其他'
        }
      },
    },
  }
</script>
